package frame.Employee;

import MyBatis.MyBatislianjian;
import frame.GuanliPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.pojo.Employee;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class employeeselect extends JInternalFrame {
    private JTextField[] inputFields = new JTextField[7]; // 输入框数组
    private JLabel[] inputLabels = new JLabel[7]; // 输入框说明标签数组
    private JTable resultTable; // 显示查询结果的表格
    private DefaultTableModel tableModel;
    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

    public employeeselect() {
        setTitle("查询员工");
        setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建顶部的查询输入区域
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 10, 5, 10); // 设置组件间距

        String[] labels = {"员工编号", "员工姓名", "密码", "等级", "电话", "薪水", "备注"};

        for (int i = 0; i < labels.length; i++) {
            inputLabels[i] = new JLabel(labels[i] + ": ");
            inputLabels[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 0;
            gbc.gridy = i;
            inputPanel.add(inputLabels[i], gbc);

            inputFields[i] = new JTextField(15);
            inputFields[i].setFont(new Font("微软雅黑", Font.PLAIN, 16));
            gbc.gridx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            inputPanel.add(inputFields[i], gbc);

            // 添加输入提示标签
            JLabel promptLabel = new JLabel("输入你知道的员工的相关信息查询");
            promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            promptLabel.setForeground(Color.GRAY);
            gbc.gridx = 2;
            gbc.insets = new Insets(5, 10, 5, 10); // 重新设置间距
            inputPanel.add(promptLabel, gbc);
        }

        add(inputPanel, BorderLayout.NORTH);

        // 创建表格模型
        String[] columnNames = {"员工编号", "员工姓名", "密码", "等级", "电话", "薪水", "备注"};
        tableModel = new DefaultTableModel(columnNames, 0);
        resultTable = new JTable(tableModel);
        resultTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        resultTable.setRowHeight(25);

        // 添加表格到滚动面板
        JScrollPane scrollPane = new JScrollPane(resultTable);
        add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton searchButton = new JButton("搜索");
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                performSearch();
            }
        });
        buttonPanel.add(searchButton);

        JButton returnButton = new JButton("返回");
        returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                GuanliPanel.setContent(new EmployeePanel());
            }
        });
        buttonPanel.add(returnButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 设置窗口大小并显示
        pack();
        setVisible(true);
    }

    // 执行搜索的方法
    private void performSearch() {
        Employee employeeQuery = new Employee();
        employeeQuery.setEid(inputFields[0].getText());
        employeeQuery.setEName(inputFields[1].getText());
        employeeQuery.setEPas(inputFields[2].getText());
        employeeQuery.setElevel(inputFields[3].getText());
        employeeQuery.setEtelPhone(inputFields[4].getText());

        // 薪水字段为整数类型，需要特殊处理
        String salaryText = inputFields[5].getText();
        Integer eSalary = null;
        if (!salaryText.isEmpty()) {
            try {
                eSalary = Integer.parseInt(salaryText);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "薪水字段必须是整数！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        employeeQuery.setESalary(eSalary);

        employeeQuery.setOther(inputFields[6].getText());

        List<Employee> employees = employeeMapper.selectEmployee(employeeQuery);

        if (employees.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有符合条件的员工信息！", "提示", JOptionPane.INFORMATION_MESSAGE);
        }

        updateTableData(employees);
    }

    // 更新表格数据的方法
    private void updateTableData(List<Employee> employees) {
        // 清空表格
        tableModel.setRowCount(0);

        // 添加新的数据
        for (Employee employee : employees) {
            Object[] row = {
                    employee.getEid(), employee.getEName(), employee.getEPas(), employee.getElevel(),
                    employee.getEtelPhone(), employee.getESalary(), employee.getOther()
            };
            tableModel.addRow(row);
        }
    }
}
